package com.hq.autotesttools.Adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hq.autotesttools.DeviceControlActivity;
import com.hq.autotesttools.MainActivity;
import com.hq.autotesttools.R;

import java.util.ArrayList;

public class DeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private ArrayList<Integer> mLeDevicesRssi;
    private LayoutInflater mInflator;
    private Context mContext;

    public DeviceListAdapter(Context context) {
        super();
        mContext = context;
        mLeDevices = new ArrayList<BluetoothDevice>();
        mLeDevicesRssi = new ArrayList<Integer>();
        mInflator = LayoutInflater.from(context);
    }

    public void addDevice(BluetoothDevice dev, Integer rssi) {
//        boolean save = true;
//        for (BluetoothDevice device:mLeDevices) {
//            if (device.getName() != null) {
//                if (device.getName().toString().compareTo(dev.getName().toString()))
//            } else {
//                device.getAddress().toString().compareTo(dev.getAddress().toString());
//            }
//        }

        Log.d("test", "addDevice: " + rssi);

        if (!mLeDevices.contains(dev)) {
            mLeDevices.add(dev);
            mLeDevicesRssi.add(rssi);
        }
    }

    public BluetoothDevice getDevice(int i) {
        return mLeDevices.get(i);
    }

    public void clear() {
        mLeDevices.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i) {
        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView deviceRssi;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = mInflator.inflate(R.layout.devicelist, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        @SuppressLint("MissingPermission")
        final String deviceName = device.getName();
        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);
        else
            viewHolder.deviceName.setText(R.string.unknown_device);

        viewHolder.deviceAddress.setText(device.getAddress());

        Integer rssi = mLeDevicesRssi.get(i);

        if (rssi >= -45) viewHolder.deviceRssi.setTextColor(ContextCompat.getColor(mContext, R.color.level5));
        else if(rssi >= -55) viewHolder.deviceRssi.setTextColor(ContextCompat.getColor(mContext, R.color.level4));
        else if(rssi >= -65) viewHolder.deviceRssi.setTextColor(ContextCompat.getColor(mContext, R.color.level3));
        else if(rssi >= -75) viewHolder.deviceRssi.setTextColor(ContextCompat.getColor(mContext, R.color.level2));
        else viewHolder.deviceRssi.setTextColor(ContextCompat.getColor(mContext, R.color.level1));

        viewHolder.deviceRssi.setText(rssi + "db");

        return view;
    }
}
